package com.czxy.controller;

import com.czxy.common.vo.PageResult;
import com.czxy.common.vo.Result;
import com.czxy.pojo.TQuestion;
import com.czxy.service.QuestionService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题目
 *
 * @author likk
 * @create 2018/8/3
 */
@RestController
@RequestMapping("/question")
@Slf4j
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 新增题目
     *
     * @param question
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public Result add(@RequestBody TQuestion question) {
        try {
            // 设置初始化信息
            question.setStatus(1); //启用

            questionService.save(question);

            return Result.build(200, "新增成功！");
        } catch (Exception e) {
            log.error("新增题目信息失败,错误信息：" + e.getMessage());

            return Result.build(500, "新增失败！");
        }
    }

    /**
     * 更新题目信息
     *
     * @param question
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Result update(@RequestBody TQuestion question) {
        try {
            questionService.updateSelective(question);

            return Result.build(200, "修改成功！");
        } catch (Exception e) {
            log.error("更新题目信息失败,错误信息：" + e.getMessage());

            return Result.build(500, "修改失败！");
        }
    }

    /**
     * 查询题目详情
     *
     * @param id
     * @return
     */
    @RequestMapping("{id}")
    public Result queryQuestionById(@PathVariable("id") Integer id) {
        try {

            TQuestion question = questionService.queryById(id);

            return Result.ok(question);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(500, "查询失败！");
        }
    }

    /**
     * 分页查询题目列表
     *
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public Result queryQuestionList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                    @RequestParam(value = "rows", defaultValue = "10") Integer rows) {

        try {
            // 设置查询条件
            PageInfo<TQuestion> pageInfo = questionService.queryPageListAndSort(page, rows, "sort asc");

            return Result.ok(new PageResult(pageInfo.getTotal(), pageInfo.getList()));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(500, "查询失败！");
        }
    }

    /**
     * 查询关卡下的所有问题
     * @param customsPassId
     * @return
     */
    @RequestMapping(value = "customsPassQuestionList", method = RequestMethod.GET)
    public Result findQuestionListByCustomsPassId(Integer customsPassId){
        try {
            // TODO 用户id
            Integer userId = 1;
            // 设置查询条件
            List<TQuestion> list = questionService.findQuestionListByCustomsPassId(customsPassId, userId);

            return Result.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.build(500, "查询失败！");
        }
    }

    /**
     * 逻辑删除题目
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    public Result deleteUser(@PathVariable("id") Integer id) {
        try {
            TQuestion record = new TQuestion();
            record.setId(id);
            record.setStatus(0); //逻辑删除，设置为停用

            questionService.updateSelective(record);

            return Result.build(200, "删除成功！");
        } catch (Exception e) {
            log.error("删除题目失败,错误信息：" + e.getMessage());

            return Result.build(500, "删除失败！");
        }
    }

}
